package com.doudou.pay.helper.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doudou.pay.helper.commons.DateTimeTool;
import com.doudou.pay.helper.domain.GetOrdersDo;
import com.doudou.pay.helper.domain.OrderItem;
import com.doudou.pay.helper.entities.PayOrder;
import com.doudou.pay.helper.mapper.PayOrderMapper;
import com.doudou.pay.helper.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private PayOrderMapper payOrderMapper;

	@Override
	public GetOrdersDo orderList(long page, long limit) {
		GetOrdersDo getOrdersDo = new GetOrdersDo();
		getOrdersDo.setList(new ArrayList<OrderItem>());

		Date nowTtime = new Date();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("firstPos", (page - 1) * limit);
		map.put("pageSize", limit);

		PayOrder payorder = new PayOrder();
		payorder.setUserId("test");
		long totalCount = payOrderMapper.selectCount(payorder);
		getOrdersDo.setTotalCount(totalCount);
		getOrdersDo.setPages(totalCount % limit == 0 ? (totalCount / limit)
				: (totalCount / limit + 1));
		List<PayOrder> list = payOrderMapper.selectForPage("test", (page - 1)
				* limit, limit);
		for (PayOrder order : list) {
			OrderItem orderItem = new OrderItem();
			orderItem.setAuthToken(order.getAuthToken());
			orderItem.setId(order.getId());
			orderItem.setMoney(order.getMoney());
			orderItem.setPayId(order.getPayId());
			orderItem
					.setPayTime(DateTimeTool.dateFormat("", order.getPayTime()));
			orderItem.setPlatformOrder(order.getPlatformOrder());
			orderItem.setPlatformOrderStatus(order.getPlatformOrderStatus());
			orderItem.setRemark(order.getRemark());
			orderItem.setStatus(order.getStatus());
			if (("1".equals(order.getStatus()))
					&& ((order.getPayTime().getTime() + 300 * 1000) < nowTtime
							.getTime())) {
				orderItem.setStatus("6");
			}
			orderItem.setType(order.getType());
			orderItem.setUserId(order.getUserId());
			getOrdersDo.getList().add(orderItem);
		}
		return getOrdersDo;
	}
}
